/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.employee.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.employee.entity.Checkinout;
import com.thinkgem.jeesite.modules.employee.entity.Employee;
import com.thinkgem.jeesite.modules.employee.service.CheckinoutService;
import com.thinkgem.jeesite.modules.employee.service.EmployeeService;

/**
 * 打卡记录Controller
 * @author pan.zhou
 * @version 2017-07-19
 */
@Controller
@RequestMapping(value = "${adminPath}/employee/checkinout")
public class CheckinoutController extends BaseController {

	@Autowired
	private CheckinoutService checkinoutService;
	
	@Autowired
	private EmployeeService  employeeService;
	
	@ModelAttribute
	public Checkinout get(@RequestParam(required=false) String id) {
		Checkinout entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = checkinoutService.get(id);
		}
		if (entity == null){
			entity = new Checkinout();
		}
		return entity;
	}
	
	@RequiresPermissions("employee:checkinout:view")
	@RequestMapping(value = {"list", ""})
	public String list(Checkinout checkinout, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Checkinout> page = checkinoutService.findPage(new Page<Checkinout>(request, response), checkinout); 
		model.addAttribute("page", page);
		return "modules/employee/checkinoutList";
	}

	@RequiresPermissions("employee:checkinout:view")
	@RequestMapping(value = "form")
	public String form(Checkinout checkinout, Model model) {
		model.addAttribute("checkinout", checkinout);
		return "modules/employee/checkinoutForm";
	}

	@RequiresPermissions("employee:checkinout:edit")
	@RequestMapping(value = "save")
	public String save(Checkinout checkinout, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, checkinout)){
			return form(checkinout, model);
		}
		Employee employee = employeeService.get(checkinout.getEmployee().getId());
		if(employee!=null){
		    Employee employee2 = new Employee();
		    employee2.setNumber(employee.getNumber());
		    employee2.setName(employee.getName());
		    checkinout.setEmployee(employee2);
		}
		checkinoutService.save(checkinout);
		addMessage(redirectAttributes, "保存打卡记录成功");
		return "redirect:"+Global.getAdminPath()+"/employee/checkinout/?repage";
	}
	
	@RequiresPermissions("employee:checkinout:edit")
	@RequestMapping(value = "delete")
	public String delete(Checkinout checkinout, RedirectAttributes redirectAttributes) {
		checkinoutService.delete(checkinout);
		addMessage(redirectAttributes, "删除打卡记录成功");
		return "redirect:"+Global.getAdminPath()+"/employee/checkinout/?repage";
	}

}